package com.readboottest.controller;

import com.readboottest.entity.Bookmark;
import com.readboottest.entity.response.BaseResponse;
import com.readboottest.service.BookmarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

import static com.readboottest.constant.BookmarkConstants.*;
import static com.readboottest.constant.GlobalConstants.FAIL;
import static com.readboottest.constant.GlobalConstants.SUCCESS;

/**
 * @author simaduoduo
 * @date 2019/7/25 14:49
 * @desc 书签模块
 */
@RequestMapping("/rfy/bookmark/")
@RestController
public class BookmarkController {
    @Autowired
    private BookmarkService bookmarkService;

    @PostMapping("/add")
    @ResponseBody
    public BaseResponse addBookmark(@RequestBody Bookmark bookmark){
        BaseResponse baseResponse = new BaseResponse();
        int code = bookmarkService.addBookmark(bookmark);
        if (code == -1){
            baseResponse.setStatus(FAIL);
            baseResponse.setMessage(BOOKMARK_NUMBER_FULL);
        }else {
            baseResponse.setStatus(SUCCESS);
            baseResponse.setMessage(SAVE_BOOKMARK_SUCCESS);
        }
        return baseResponse;
    }

    @PostMapping("/delete")
    @ResponseBody
    public BaseResponse deleteBookmark(@RequestParam(name = "id") int id ){
        BaseResponse baseResponse = new BaseResponse();
        if (bookmarkService.removeBookmark(id) == 0){
            baseResponse.setStatus(FAIL);
            baseResponse.setMessage(DELETE_BOOKMARK_FAIL);
        }else {
            baseResponse.setStatus(SUCCESS);
            baseResponse.setMessage(DELETE_BOOKMARK_SUCCESS);
        }
        return baseResponse;
    }

    @PostMapping("/update")
    @ResponseBody
    public BaseResponse updateBookmark(@RequestBody Bookmark bookmark){
        BaseResponse baseResponse = new BaseResponse();
        if (bookmarkService.updateBookmark(bookmark) == 0){
            baseResponse.setStatus(FAIL);
            baseResponse.setMessage(UPDATE_BOOKMARK_FAIL);
        }else {
            baseResponse.setStatus(SUCCESS);
            baseResponse.setMessage(UPDATE_BOOKMARK_SUCCESS);
        }
        return baseResponse;
    }

    @PostMapping("/query")
    @ResponseBody
    public BaseResponse queryBookmarks(@RequestParam(name = "userId") int uesrId){
        BaseResponse baseResponse = new BaseResponse();
        List<Bookmark> result = bookmarkService.queryBookmarks(uesrId);
        baseResponse.setStatus(SUCCESS);
        baseResponse.setResult(result);
        return baseResponse;
    }
}
